/**
 * Created by 83617 on 2019/1/2.
 */
var mongoose = require('mongoose')
const Schema = mongoose.Schema;
var societySchema = new mongoose.Schema({
    name: String,
    num: String,
    icon: String,
    master: String,
    detail: String,
    activities:[],
    meta:{
        createAt: {
            type: Date,
        default: Date.now()
        },
        updateAt: {
            type: Date,
        default: Date.now()
        }
    }
})

societySchema.pre('save',function(next){
    if (this.isNew) {
        this.meta.createAt = this.meta.updateAt = Date.now();
    } else {
        this.meta.updateAt = Date.now();
    }
    next()
})

societySchema.statics={
    fetch:function(cb) {
        return this
            .find({})
            .sort('meta.updateAt')
            .exec(cb)
    },
    findIdByName:function(name,cb){
        return this
            .findOne({name:name},'_id')
            .exec(cb)
    },
    findSingleCol:function(col,cb) {
        return this
            .find({}, col)
            .sort('meta.updateAt')
            .exec(cb)
    },
    deleteById:function(id,cb){
        return this
            .findOneAndDelete({_id:id})
            .exec(cb)
    },
    findOneById:function(id,cb){
        return this
            .findOne({_id:id})
            .exec(cb)
    }
}

module.exports=societySchema
